import os
import sys
import time
import urllib.parse as parse

import requests
from xjlibrary.our_file_dir import BaseDir

curPath = BaseDir.get_file_dir_absolute(__file__)
TopPath = BaseDir.get_upper_dir(curPath, -3)
sPath = BaseDir.get_new_path(TopPath, "download", "cnki_bz", "download")

insertpath = os.path.dirname(os.path.abspath(__file__))
print(insertpath)
sys.path.insert(0, insertpath)

# listproxy = RingList()

class_dict = {'A': '综合', 'B': '农业、林业', 'C': '医药、卫生、劳动保护', 'D': '矿业', 'E': '石油', 'F': '能源、核技术', 'G': '化工',
              'H': '冶金', 'J': '机械', 'K': '电工', 'L': '电子元器件与信息技术', 'M': '通信、广播', 'N': '仪器、仪表', 'P': '土木、建筑', 'Q': '建材',
              'R': '公路、水路运输', 'S': '铁路', 'T': '车辆', 'U': '船舶', 'V': '航空、航天', 'W': '纺织', 'X': '食品', 'Y': '轻工、文化与生活用品',
              'Z': '环境保护'}
HtmlYearRoot = os.path.join(sPath, 'year_html')

Proxies = {
    'http': '192.168.30.4:8080',
    'https': '192.168.30.4:8080'  # key是指目标网站的协议
}

UserAgent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36'


# 保存年份
def DownYear(NaviCode):
    outPathFile = os.path.join(HtmlYearRoot, NaviCode + '.htm')
    if BaseDir.is_file_exists(outPathFile):
        print("文件存在{}".format(outPathFile))
        return
    # proxies = get_proxys()
    sn = requests.Session()
    sn.headers['User-Agent'] = UserAgent
    url = 'http://kns.cnki.net/kns/brief/result.aspx?dbPrefix=CISD'
    print('input url:%s' % url)

    r = sn.get(url,
               proxies=Proxies,
               timeout=(10, 30))
    # r = sn.get(url, timeout=10)
    print(r)

    timestring = time.strftime("%a %b %d %Y %H:%M:%S ")
    timestring = "{}{}({})".format(parse.quote(timestring), parse.quote("GMT+0800 "), parse.quote("中国标准时间"))
    print(timestring)

    url = 'http://kns.cnki.net/kns/request/SearchHandler.ashx?action='
    url += '&NaviCode=' + NaviCode
    url += '&action=&catalogName=SCSD_CCSCLS&ua=1.21&PageName=ASP.brief_result_aspx&DbPrefix=CISD&DbCatalog=%e6%a0%87%e5%87%86%e6%95%b0%e6%8d%ae%e6%80%bb%e5%ba%93&ConfigFile=CISD.xml&db_opt=CISD&his=0'
    url += '&__=' + timestring
    print('input url:%s' % url)
    # r = sn.get(url, timeout=10)
    r = sn.get(url,
               proxies=Proxies,
               timeout=(10, 30))
    print(r)
    print(r.text)
    # url = '''http://kns.cnki.net/kns/group/doGroupLeft.aspx?action=1&Param=ASP.brief_result_aspx#CISD/年/年,count(*)/年/(年,'date')#年$desc/1000000$/-/40/40000/ButtonView&cid=0&clayer=0'''
    url = 'http://kns.cnki.net/kns/group/doGroupLeft.aspx?' \
          'action=1&Param=ASP.brief_result_aspx%23CISD/%u5E74/%u5E74%2Ccount%28*%29/%u5E74/%28%u5E74%2C%27date%27%29%23%u5E74%24desc/1000000%24/-/40/40000/ButtonView&cid=0&clayer=0' \
          '&__=' + timestring

    # req = requests.Request('GET', url)
    # prepped = sn.prepare_request(req)
    # prepped.url = prepped.url.replace('%25', '%')
    # r = sn.send(prepped)

    url = 'http://kns.cnki.net/kns/group/doGroupLeft.aspx?' + parse.urlencode(
        dict(parse.parse_qsl(parse.urlparse(url).query)))
    print('input url:%s' % url)
    r = sn.get(url,
               proxies=Proxies,
               timeout=(10, 30))
    # r = sn.get(url, timeout=10)
    print(r)
    # print(r.url)
    if not os.path.exists(HtmlYearRoot):
        os.makedirs(HtmlYearRoot)

    print('outPathFile:%s' % outPathFile)
    with open(outPathFile, mode='wb') as f:
        f.write(r.content)


def main(logger=None):
    StartTime = time.time()
    # sorted() 函数对所有可迭代的对象进行排序操作
    for keys in sorted(class_dict.keys()):
        print(keys + ':' + class_dict[keys])
        try:
            DownYear(keys)
        except:
            pass

    print('Time total:' + repr(time.time() - StartTime) + '\n')


if __name__ == '__main__':
    main()
